package com.cn.medicalcare.controller.backstageservlet.news;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.NewsCategory;
import com.cn.medicalcare.service.NewsCategoryService;
import com.cn.medicalcare.service.serviceimpl.NewsCategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/news/insertnewscategory.json")
public class NewsCategoryInsertServlet extends HttpServlet {

    private NewsCategoryService newsCategoryService = NewsCategoryServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String survival = req.getParameter("survival");

        NewsCategory newsCategory = new NewsCategory();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            newsCategory.setSurvival(Integer.valueOf(survival)).setName(name);
            if (newsCategoryService.insertNewsCategory(newsCategory)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
